using System.Text.RegularExpressions;

namespace HuaXianQu.ProtoBuffEx.ToolEditor
{
    public class ProtoListTypeParse : IProtoTypeParse
    {
        private ProtoBasicTypeParse _basicTypeParse = new ProtoBasicTypeParse();

        public bool ParseType(string type, out string protoType)
        {
            protoType = null;
            type = type.Trim();
            Match match = Regex.Match(type, @"List<([^>]+)>");
            string data = match.Groups[1].Value;

            if (!string.IsNullOrEmpty(data))
            {
                if (_basicTypeParse.ParseType(data, out protoType))
                {
                    protoType = $"repeated {protoType}";
                }
                else
                {
                    protoType = $"repeated {data}";
                }

                return true;
            }

            return false;
        }
    }
}